package top.infopub.mgr.cap.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.cap.dao.ContractAttDao;
import top.infopub.mgr.cap.dao.ContractBankDao;
import top.infopub.mgr.cap.dao.ContractInfoDao;
import top.infopub.mgr.cap.domain.ContractInfoDomain;
import top.infopub.model.cap.ContractAtt;
import top.infopub.model.cap.ContractBank;
import top.infopub.model.cap.ContractInfo;
import top.infopub.mgr.cap.service.ContractInfoService;
import top.infopub.mgr.cap.vo.ContractInfoVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;

@Service
public class ContractInfoServiceImpl extends GenericServiceImpl<ContractInfo, String> implements
		ContractInfoService {

	@Autowired
	private ContractInfoDao contractInfoDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Autowired
	private ContractBankDao contractBankDao;
	
	@Autowired
	private ContractAttDao contractAttDao;
	
	@Override
	public GenericDao<ContractInfo, String> getDao() {
		return contractInfoDao;
	}

    @Override
    public Page<ContractInfoDomain> selectByDomainWhere(Page<ContractInfoDomain> page,
                                                        ContractInfoVo contractInfoVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ContractInfoDomain>)contractInfoDao.selectByDomainWhere(contractInfoVo);
    }

    @Override
    public Page<ContractInfoDomain> selectMyCheckByDomainWhere(Page<ContractInfoDomain> page,
                                                               ContractInfoVo contractInfoVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ContractInfoDomain>)contractInfoDao.selectMyCheckByDomainWhere(contractInfoVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveContractInfo(ContractInfo contractInfo, List<ContractBank> contractBanks,
                             List<ContractAtt> contractAtts) {
        //执行新增
        //添加主表信息
        contractInfoDao.insertById(contractInfo);
        
        //批量添加金融机构信息
        if(CollectionUtils.isNotEmpty(contractBanks)){
            contractBankDao.insertByBatch(contractBanks);
        }
        
        //批量添加融资合同附件
        if(CollectionUtils.isNotEmpty(contractAtts)){
            contractAttDao.insertByBatch(contractAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.CAP_CONTRACT_INFO;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, contractInfo.getId(), contractInfo.getProcessId(), null);
        
    }

    @Override
    public ContractInfoVo selectById(String id) {
        return contractInfoDao.selectById(id);
    }

    @Override
    public ContractInfo isExistsByContractName(String contractName) {
        return contractInfoDao.isExistsByContractName(contractName);
    }

    @Override
    public List<ContractInfo> queryByProCode(String proId) {
        return contractInfoDao.queryByProCode(proId);
    }

    @Override
    public List<ContractInfo> queryAll() {
        return contractInfoDao.queryAll();
    }

}